package com.bl.weilan.api.controller.news;
import com.bl.weilan.common.ApiResponseBody;
import com.bl.weilan.api.controller.BaseController;
import com.bl.weilan.dao.model.NewsEntity;
import com.bl.weilan.service.biz.news.NewsService;
import com.bl.weilan.service.vo.news.NewsListVo;
import io.swagger.annotations.Api;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
/**
 * Created by liangweikang on 18/12/26.
 */
@RestController
@Log4j2
@RequestMapping(value = {"/weilan/apis/news"})
@Api(value = "新闻相关", description = "新闻相关api")
public class NewsController extends BaseController {

    @Autowired
    NewsService newsService;

    @ApiOperation(value = "获取新闻列表,type类型-非必填，ALL或空返回所有的新闻 MARKET 市场 LEARNING 学术 POLICY 政策", notes = "获取新闻列表,type类型-非必填，ALL或空返回所有的新闻")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型", required = false, paramType = "query", dataType = "String", defaultValue = "ALL"),
            @ApiImplicitParam(name = "page", value = "页数", required = false, paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页大小", required = false, paramType = "query", dataType = "int", defaultValue = "10")})
    @GetMapping(value = {"/get_news_list"})
    public ApiResponseBody<NewsListVo> newsList(
            @RequestParam(value = "type", required = false, defaultValue = "ALL") String type,
            @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @RequestParam(value = "size", required = false, defaultValue = "10") int size,
            HttpServletRequest request) {

        Pageable pageable = new PageRequest(page - 1, size);

        NewsListVo newsListVo = newsService.getNewsLists(type, pageable);

        return ApiResponseBody.createSuccessBody(newsListVo);
    }


    @ApiOperation(value = "获取一条新闻详情信息", notes = "获取一条新闻")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")})
    @GetMapping(value = {"/get_news_byid"})
    public ApiResponseBody<NewsListVo> newsList(
            @RequestParam(value = "id", required = true) String id,
            HttpServletRequest request) {

        NewsEntity newsEntity = newsService.getNewsById(id);

        return ApiResponseBody.createSuccessBody(newsEntity);
    }
}
